package com.boarsoft.common.util;

import java.util.List;

import com.boarsoft.common.Util;

public class StringUtil {
	/**
	 * 判断a，b两个字符是否相等
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean equals(String a, String b) {
		if (a == null) {
			return b == null;
		}
		return a.equals(b);
	}

	/**
	 * 字符串是否为空
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s) {
		return (s == null || "".equals(s));
	}

	/**
	 * 字符串不空
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNotEmpty(String s) {
		return (s != null && !"".equals(s));
	}

	public static String substring(String str, int start, int end) {
		if (str == null) {
			return null;
		} else {
			if (end < 0) {
				end += str.length();
			}
			if (start < 0) {
				start += str.length();
			}
			if (end > str.length()) {
				end = str.length();
			}
			if (start > end) {
				return "";
			} else {
				if (start < 0) {
					start = 0;
				}
				if (end < 0) {
					end = 0;
				}
				return str.substring(start, end);
			}
		}
	}

	public static boolean isBlank(String str) {
		int length;
		if (str != null && (length = str.length()) != 0) {
			for (int i = 0; i < length; ++i) {
				if (!Character.isWhitespace(str.charAt(i))) {
					return false;
				}
			}
			return true;
		} else {
			return true;
		}
	}

	public static boolean isNotBlank(String s) {
		return !isBlank(s);
	}

	public static boolean isBlank(CharSequence cs) {
		int length;
		if (cs != null && (length = cs.length()) != 0) {
			for (int i = 0; i < length; ++i) {
				if (!Character.isWhitespace(cs.charAt(i))) {
					return false;
				}
			}
			return true;
		} else {
			return true;
		}
	}

	public static boolean isNotBlank(CharSequence cs) {
		return !isBlank(cs);
	}

	/**
	 * Return whether the string is null or 0-length.
	 *
	 * @param s
	 *            The string.
	 * @return {@code true}: yes<br>
	 *         {@code false}: no
	 */
	public static boolean isEmpty(final CharSequence s) {
		return s == null || s.length() == 0;
	}

	/**
	 * Return whether the string is null or whitespace.
	 *
	 * @param s
	 *            The string.
	 * @return {@code true}: yes<br>
	 *         {@code false}: no
	 */
	public static boolean isTrimEmpty(final String s) {
		return (s == null || s.trim().length() == 0);
	}

	/**
	 * Return whether the string is null or white space.
	 *
	 * @param s
	 *            The string.
	 * @return {@code true}: yes<br>
	 *         {@code false}: no
	 */
	public static boolean isSpace(final String s) {
		if (s == null)
			return true;
		for (int i = 0, len = s.length(); i < len; ++i) {
			if (!Character.isWhitespace(s.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Return whether string1 is equals to string2.
	 *
	 * @param s1
	 *            The first string.
	 * @param s2
	 *            The second string.
	 * @return {@code true}: yes<br>
	 *         {@code false}: no
	 */
	public static boolean equals(final CharSequence s1, final CharSequence s2) {
		if (s1 == s2)
			return true;
		int length;
		if (s1 != null && s2 != null && (length = s1.length()) == s2.length()) {
			if (s1 instanceof String && s2 instanceof String) {
				return s1.equals(s2);
			} else {
				for (int i = 0; i < length; i++) {
					if (s1.charAt(i) != s2.charAt(i))
						return false;
				}
				return true;
			}
		}
		return false;
	}

	/**
	 * Return whether string1 is equals to string2, ignoring case
	 * considerations..
	 *
	 * @param s1
	 *            The first string.
	 * @param s2
	 *            The second string.
	 * @return {@code true}: yes<br>
	 *         {@code false}: no
	 */
	public static boolean equalsIgnoreCase(final String s1, final String s2) {
		return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
	}

	/**
	 * Return {@code ""} if string equals null.
	 *
	 * @param s
	 *            The string.
	 * @return {@code ""} if string equals null
	 */
	public static String null2Length0(final String s) {
		return s == null ? "" : s;
	}

	/**
	 * Return the length of string.
	 *
	 * @param s
	 *            The string.
	 * @return the length of string
	 */
	public static int length(final CharSequence s) {
		return s == null ? 0 : s.length();
	}

	/**
	 * Set the first letter of string upper.
	 *
	 * @param s
	 *            The string.
	 * @return the string with first letter upper.
	 */
	public static String upperFirstLetter(final String s) {
		if (s == null || s.length() == 0)
			return "";
		if (!Character.isLowerCase(s.charAt(0)))
			return s;
		return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
	}

	/**
	 * Set the first letter of string lower.
	 *
	 * @param s
	 *            The string.
	 * @return the string with first letter lower.
	 */
	public static String lowerFirstLetter(final String s) {
		if (s == null || s.length() == 0)
			return "";
		if (!Character.isUpperCase(s.charAt(0)))
			return s;
		return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
	}

	/**
	 * Reverse the string.
	 *
	 * @param s
	 *            The string.
	 * @return the reverse string.
	 */
	public static String reverse(final String s) {
		if (s == null)
			return "";
		int len = s.length();
		if (len <= 1)
			return s;
		int mid = len >> 1;
		char[] chars = s.toCharArray();
		char c;
		for (int i = 0; i < mid; ++i) {
			c = chars[i];
			chars[i] = chars[len - i - 1];
			chars[len - i - 1] = c;
		}
		return new String(chars);
	}

	/**
	 * Convert string to DBC.
	 *
	 * @param s
	 *            The string.
	 * @return the DBC string
	 */
	public static String toDBC(final String s) {
		if (s == null || s.length() == 0)
			return "";
		char[] chars = s.toCharArray();
		for (int i = 0, len = chars.length; i < len; i++) {
			if (chars[i] == 12288) {
				chars[i] = ' ';
			} else if (65281 <= chars[i] && chars[i] <= 65374) {
				chars[i] = (char) (chars[i] - 65248);
			}
		}
		return new String(chars);
	}

	/**
	 * Convert string to SBC.
	 *
	 * @param s
	 *            The string.
	 * @return the SBC string
	 */
	public static String toSBC(final String s) {
		if (s == null || s.length() == 0)
			return "";
		char[] chars = s.toCharArray();
		for (int i = 0, len = chars.length; i < len; i++) {
			if (chars[i] == ' ') {
				chars[i] = (char) 12288;
			} else if (33 <= chars[i] && chars[i] <= 126) {
				chars[i] = (char) (chars[i] + 65248);
			}
		}
		return new String(chars);
	}

	/**
	 * 如果s1为空，或空白字符串，则替换为s2
	 * 
	 * @param s1
	 * @param s2
	 * @return
	 */
	public static String defaultIfBlank(String s1, String s2) {
		return isBlank(s1) ? s2 : s1;
	}

	/**
	 * 将对象转换成String，如果为null，则返回defaultValue
	 * 
	 * @param o
	 * @param defaultValue
	 * @return
	 */
	public static String toString(Object o, String defaultValue) {
		if (o == null) {
			return defaultValue;
		}
		if (o instanceof String) {
			return (String) o;
		}
		return o.toString();
	}

	/**
	 * 将List<String>转换成以sp分隔的字符串
	 * 
	 * @param list
	 * @param sp
	 * @return
	 */
	public static String join(List<String> list, String sp) {
		return Util.array2str(list.toArray(), sp);
	}
}